Imports DotNetNuke.Entities.Portals

Namespace DotNetNuke.Modules.ProjectTracker

    Public Class EMailer
        Private m_HistoryController As New HistoryController
        Private m_HistoryInfo As HistoryInfo

        Public Sub StateNotification(ByVal roletype As String, ByVal project As DotNetNuke.Modules.ProjectTracker.ProjectsInfo, ByVal state As DotNetNuke.Modules.ProjectTracker.StatesInfo, ByVal status As String, ByRef aMailSent As ArrayList)

            Dim objRoleController As New DotNetNuke.Security.Roles.RoleController
            Dim objUserController As New DotNetNuke.Entities.Users.UserController
            Dim objUser As UserInfo
            Dim _subject As String
            Dim _body As String
            Dim strHistory As String = String.Empty
            Dim strHistoryItem As String = String.Empty

            Dim userController As New DotNetNuke.Entities.Users.UserController
            Dim user As DotNetNuke.Entities.Users.UserInfo = Nothing
            Dim admin As DotNetNuke.Entities.Users.UserInfo = Nothing


            Dim subscribers As ArrayList

            ' Obtain PortalSettings from Current Context
            Dim _portalSettings As PortalSettings = CType(HttpContext.Current.Items("PortalSettings"), PortalSettings)

            Try

                Select Case status
                    Case "ENTRY"
                        _subject = state.EntrySubject
                        _body = state.EntryBody
                    Case "PASS"
                        _subject = state.PassSubject
                        _body = state.PassBody
                    Case "FAIL"
                        _subject = state.FailSubject
                        _body = state.FailBody
                End Select

                If _subject.Trim().Length = 0 Or _body.Trim().Length = 0 Then
                    Return
                End If

                ' substitute data elements for any tags in the subject or body
                If _subject <> String.Empty Then
                    _subject = _subject.Replace("[PROJECTNAME]", project.ProjectName)
                    _subject = _subject.Replace("[VERSION]", project.Version)
                End If
                If _body <> String.Empty Then
                    ' project info
                    _body = _body.Replace("[PROJECTNAME]", project.ProjectName)
                    _body = _body.Replace("[VERSION]", project.Version)
                    _body = _body.Replace("[SUBMITTEDDATE]", String.Format("{0} {1}", project.CreatedDate.ToShortDateString, project.CreatedDate.ToShortTimeString))
                    _body = _body.Replace("[URL]", HttpContext.Current.Request.Url.AbsoluteUri)
                    ' download links
                    _body = _body.Replace("[PACKAGE]", GetPackageURL(project))
                    ' history info
                    For Each item As HistoryInfo In m_HistoryController.GetHistoryList(project.ItemID)
                        strHistoryItem = (String.Format("{0} {1}", item.CreatedDate.ToShortDateString, item.CreatedDate.ToShortTimeString))
                        user = userController.GetUser(_portalSettings.PortalId, item.CreatedByUser)
                        strHistoryItem &= (String.Format(" --- {0} - {1}", item.Activity, user.DisplayName) & vbCrLf)
                        strHistory &= strHistoryItem
                    Next
                    _body = _body.Replace("[HISTORY]", strHistory)
                End If

                Dim rolelist As String = String.Empty
                Select Case roletype
                    Case "AUTH"
                        rolelist = state.AuthorizedRoles
                    Case "SUBS"
                        rolelist = state.SubscriberRoles
                    Case "OWNER"
                        rolelist = ""
                End Select

                If rolelist <> String.Empty Then
                    For Each RoleId As String In rolelist.Split(CType(";", Char))
                        ' get list of role members
                        If RoleId <> "-1" And RoleId <> String.Empty Then
                            Dim RoleName As String = objRoleController.GetRole(Integer.Parse(RoleId), _portalSettings.PortalId).RoleName
                            subscribers = objRoleController.GetUserRolesByRoleName(_portalSettings.PortalId, RoleName)
                            For Each subscriber As UserRoleInfo In subscribers
                                objUser = Nothing
                                objUser = objUserController.GetUser(_portalSettings.PortalId, subscriber.UserID)
                                If Not objUser Is Nothing Then
                                    If aMailSent.IndexOf(objUser.Email) = -1 Then
                                        DotNetNuke.Services.Mail.Mail.SendMail(_portalSettings.Email, objUser.Membership.Email, "", _subject, _body, "", "text", "", "", "", "")
                                        ' add history record 
                                        m_HistoryInfo = New HistoryInfo
                                        m_HistoryInfo.ProjectID = project.ItemID
                                        m_HistoryInfo.ModuleID = state.ModuleID
                                        m_HistoryInfo.CreatedByUser = objUser.UserID
                                        m_HistoryInfo.CreatedDate = Date.Now()
                                        m_HistoryInfo.Activity = "EMAIL SENT: " & _subject
                                        m_HistoryInfo.ActivityType = "EMAIL"
                                        m_HistoryInfo.ObjectID = -1
                                        m_HistoryController.AddHistory(m_HistoryInfo)
                                        ' keep track of who emails are sent to
                                        aMailSent.Add(objUser.Email)
                                    End If
                                End If
                            Next
                        End If
                    Next
                Else
                    If roletype = "OWNER" Then
                        ' send the email to the package owner
                        objUser = Nothing
                        objUser = userController.GetUser(_portalSettings.PortalId, project.ProjectLead)
                        If Not objUser Is Nothing Then
                            If aMailSent.IndexOf(objUser.Email) = -1 Then
                                DotNetNuke.Services.Mail.Mail.SendMail(_portalSettings.Email, objUser.Membership.Email, "", _subject, _body, "", "text", "", "", "", "")
                                ' add history record 
                                m_HistoryInfo = New HistoryInfo
                                m_HistoryInfo.ProjectID = project.ItemID
                                m_HistoryInfo.ModuleID = state.ModuleID
                                m_HistoryInfo.CreatedByUser = objUser.UserID
                                m_HistoryInfo.CreatedDate = Date.Now()
                                m_HistoryInfo.Activity = "EMAIL SENT: " & _subject
                                m_HistoryInfo.ActivityType = "EMAIL"
                                m_HistoryInfo.ObjectID = -1
                                m_HistoryController.AddHistory(m_HistoryInfo)
                                ' keep track of who emails are sent to
                                aMailSent.Add(objUser.Email)
                            End If
                        End If
                    End If
                End If

                '' now, see if there are any additional emails to send the emails to
                Dim subscriberscontroller As New StateSubscribersController

                Select Case roletype
                    Case "AUTH"
                        subscribers = subscriberscontroller.GetStateSubscribersList(state.ItemID, 2)
                        For Each subscr As StateSubscribersInfo In subscribers
                            If subscr.EMail <> String.Empty Then
                                If aMailSent.IndexOf(subscr.Email) = -1 Then
                                    DotNetNuke.Services.Mail.Mail.SendMail(_portalSettings.Email, subscr.EMail, "", _subject, _body, "", "text", "", "", "", "")
                                    ' add history record 
                                    m_HistoryInfo = New HistoryInfo
                                    m_HistoryInfo.ProjectID = project.ItemID
                                    m_HistoryInfo.ModuleID = state.ModuleID
                                    m_HistoryInfo.CreatedByUser = subscr.ItemID * -1
                                    m_HistoryInfo.CreatedDate = Date.Now()
                                    m_HistoryInfo.Activity = "EMAIL SENT: " & _subject
                                    m_HistoryInfo.ActivityType = "EMAIL"
                                    m_HistoryInfo.ObjectID = -1
                                    m_HistoryController.AddHistory(m_HistoryInfo)
                                    ' keep track of who emails are sent to
                                    aMailSent.Add(subscr.Email)
                                End If
                            End If
                        Next
                    Case "SUBS"
                        subscribers = subscriberscontroller.GetStateSubscribersList(state.ItemID, 1)
                        For Each subscr As StateSubscribersInfo In subscribers
                            If subscr.EMail <> String.Empty Then
                                If aMailSent.IndexOf(subscr.Email) = -1 Then
                                    DotNetNuke.Services.Mail.Mail.SendMail(_portalSettings.Email, subscr.EMail, "", _subject, _body, "", "text", "", "", "", "")
                                    ' add history record 
                                    m_HistoryInfo = New HistoryInfo
                                    m_HistoryInfo.ProjectID = project.ItemID
                                    m_HistoryInfo.ModuleID = state.ModuleID
                                    m_HistoryInfo.CreatedByUser = subscr.ItemID * -1
                                    m_HistoryInfo.CreatedDate = Date.Now()
                                    m_HistoryInfo.Activity = "EMAIL SENT: " & _subject
                                    m_HistoryInfo.ActivityType = "EMAIL"
                                    m_HistoryInfo.ObjectID = -1
                                    m_HistoryController.AddHistory(m_HistoryInfo)
                                    ' keep track of who emails are sent to
                                    aMailSent.Add(subscr.Email)
                                End If
                            End If
                        Next
                End Select

            Catch objException As Exception
                ' mail configuration problem
                LogException(objException.InnerException)
            End Try

            Return

        End Sub

        Private Function GetPackageURL(ByVal project As ProjectsInfo) As String
            ' Obtain PortalSettings from Current Context
            Dim _portalSettings As PortalSettings = CType(HttpContext.Current.Items("PortalSettings"), PortalSettings)
            Dim rootPath As String = HttpContext.Current.Request.ServerVariables("APPL_PHYSICAL_PATH")
            Dim sPath As String = HttpContext.Current.Server.MapPath(ResolveUrl(String.Format("~/Portals/{0}/Projects/{1}", _portalSettings.PortalId, project.Url)))
            Return "http://" & HttpContext.Current.Request.ServerVariables("HTTP_HOST") & ResolveUrl("~/" & Replace(Right(sPath, Len(sPath) - Len(rootPath)), "\", "/"))
        End Function

    End Class

End Namespace
